package com.pencilisland.pay.core.mp.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pencilisland.pay.api.bean.constant.GoodsConstant;
import com.pencilisland.pay.api.bean.mp.core.MessageRequest;
import com.pencilisland.pay.api.bean.mp.core.MessageResponse;
import com.pencilisland.pay.api.bean.mp.core.PublishGoodsREQ;
import com.pencilisland.pay.api.bean.mp.core.PublishGoodsRES;
import com.pencilisland.pay.api.entity.GoodsEntity;
import com.pencilisland.pay.api.entity.GoodsImgEntity;
import com.pencilisland.pay.api.repository.GoodsImgRepository;
import com.pencilisland.pay.api.repository.GoodsRepository;
import com.pencilisland.pay.api.service.CacheService;
import com.pencilisland.pay.core.mp.service.MBService;

/**
 * 报文逻辑处理.发布商品
 *
 * @author breeze
 */
@Service("MB_publishGoods")
public class PublishGoodsMB implements MBService {
	@Autowired
	GoodsRepository goodsRepo;
	@Autowired
	GoodsImgRepository goodsImgRepo;
	@Autowired
	CacheService cacheService;
	@Autowired
	ObjectMapper objectMapper;

	@Override
	@Transactional
	public MessageResponse process(MessageRequest request) {
		PublishGoodsREQ requestBean = getRequestBody(request);

		GoodsEntity goods = goodsRepo.findByCode(requestBean.getCode());
		if (goods == null) {
			goods = new GoodsEntity();
			goods.setCode(requestBean.getCode());
		}
		goods.setMerchantId(requestBean.getMerchantId());
		goods.setName(requestBean.getName());
		goods.setPrice(requestBean.getPrice());
		goods.setRemark(requestBean.getRemark());
		goods.setStock(requestBean.getStock());
		goods.setFreePost(requestBean.getFreePost());
		goods.setWeight(requestBean.getWeight());
		goods.setStatus(GoodsConstant.STATUS_1);
		goods.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));

		goodsRepo.save(goods);

		goodsImgRepo.deleteByGoodsId(goods.getId());
		if (requestBean.getImgDetails() != null && !requestBean.getImgDetails().isEmpty()) {
			List<GoodsImgEntity> goodsImgs = new ArrayList<GoodsImgEntity>();
			int count = 0;
			for (PublishGoodsREQ.ElementImgDetails imgDetail : requestBean.getImgDetails()) {
				GoodsImgEntity goodsImg = new GoodsImgEntity();
				goodsImg.setGoodsId(goods.getId());
				goodsImg.setUrl(imgDetail.getUrl());
				goodsImg.setType(imgDetail.getType());
				goodsImg.setWeight(count);
				count++;
				goodsImgs.add(goodsImg);
			}

			goodsImgRepo.save(goodsImgs);
		}

		cacheService.clearGoods(goods.getCode());
		cacheService.clearAllGoods();

		PublishGoodsRES res = new PublishGoodsRES(true, null, "OK");
		return res;
	}

	public PublishGoodsREQ getRequestBody(MessageRequest request) {
		
		return objectMapper.convertValue(request.getBody(), PublishGoodsREQ.class);
		
	}

}
